﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;

namespace BemEstarEmprego.Web.Helpers
{
	/// <summary>
	/// Modal Crud - Modal de inclusão de registros
	/// </summary>
	public class ModalCrud : IModalCrud
	{

		#region Propriedades

		private String btnCrud;
		private String divCrud;

		private String modalCrudTitle;
		private String modalButtonName;
		private object htmlAttributes;
		private Type tipoModal;

		#endregion

		#region Construtor

		/// <summary>
		/// Create a ModalCrud
		/// </summary>
		/// <param name="htmlAttributes"></param>
		public ModalCrud(object htmlAttributes, Type t)
		{
			this.htmlAttributes = htmlAttributes;
		}

		#endregion

		#region Options

		public IModalCrudOptions BtnCrud(string btnCrud)
		{
			this.btnCrud = btnCrud;
			return new ModalCrudOptions(this);
		}

		public IModalCrudOptions DivCrud(string divCrud)
		{
			this.divCrud = divCrud;
			return new ModalCrudOptions(this);
		}

		public IModalCrudOptions ModalTitle(string modalTitle)
		{
			this.modalCrudTitle = modalTitle;
			return new ModalCrudOptions(this);
		}

		public IModalCrudOptions ModalButtonName(string modalButtonName)
		{
			this.modalButtonName = modalButtonName;
			return new ModalCrudOptions(this);
		}

		#endregion

		#region ToString

		//Render HTML
		public override string ToString()
		{
			return RenderAlert();
		}

		//Return ToString
		public string ToHtmlString()
		{
			return ToString();
		}

		#endregion

		#region Render

		private string RenderAlert()
		{

			//SPEC
			//		<div id='ctn-ModalCrud'>
			//			<!-- Button trigers the Modal Crud -->
			//			<a id='btnCrd1' href='#' onclick='loadModal('btnCrd1','Crd1')' class='btn'>Incentivo</a>
			//			
			//			<!-- Modal Crud -->
			//			<div id='Crd1' title='Teste' class='ui-dialog' style='display: none;'>
			//					Teste
			//			</div>
			//		</div>


			//<div id='ctn-ModalCrud'>
			TagBuilder wrapper = new TagBuilder("div");

			//merge attributes
			wrapper.MergeAttributes(htmlAttributes != null ? HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes) : null);

			wrapper.InnerHtml += RenderBtnCrd();
			wrapper.InnerHtml += RenderDivCrd();

			String teste = wrapper.ToString();

			return wrapper.ToString();
		}
		private String RenderBtnCrd()
		{

			String loadModal = "loadModal('" + btnCrud + "','" + divCrud + "')";

			TagBuilder btnCrd = new TagBuilder("a");

			btnCrd.Attributes.Add("id", btnCrud);
			btnCrd.Attributes.Add("onclick", loadModal);
			btnCrd.AddCssClass("btn");

			//Buton name
			btnCrd.InnerHtml += !String.IsNullOrWhiteSpace(modalButtonName) ? modalButtonName : "Nome não definido!";

			return btnCrd.ToString();
		}
		private String RenderDivCrd()
		{
			TagBuilder btnDivCrd = new TagBuilder("div");

			btnDivCrd.Attributes.Add("id", divCrud);
			btnDivCrd.Attributes.Add("title", !String.IsNullOrWhiteSpace(modalCrudTitle) ? modalCrudTitle : "Título Não Atribuído!");
			btnDivCrd.Attributes.Add("style", "display: none;");

			btnDivCrd.InnerHtml += "Teste";

			return btnDivCrd.ToString();
		}

		private String RenderTableContent()
		{
			TagBuilder btnDivCrd = new TagBuilder("div");

			btnDivCrd.Attributes.Add("id", divCrud);
			btnDivCrd.Attributes.Add("title", !String.IsNullOrWhiteSpace(modalCrudTitle) ? modalCrudTitle : "Título Não Atribuído!");
			btnDivCrd.Attributes.Add("style", "display: none;");

			btnDivCrd.InnerHtml += "Teste";

			return btnDivCrd.ToString();
		}

		private void GetTableContent()
		{
			
		}

		#endregion

	}
}